const fs = require('fs')
const path = require('path')
const db = require('./db')
function readMyfile(name) {
    return new Promise((resolve, reject) => {
        fs.readFile(path.join(__dirname, `../assets/${name}.txt`), 'utf-8',(err, data) => {
            if(err) throw err
            resolve(data)
        })
    })
    
}

let fn = async () => {

    let reactText = await readMyfile('rect')
    let vueText = await readMyfile('vue')
    let angularText = await readMyfile('angular')

    let data = [
        {id: 0, title: "一套框架多种平台 移动端&桌面端", author: "张三丰", date: "2013-03-22", imgUrl: "/images/dt.png", content: angularText},
        {id: 1, title: "渐进式的JavaScript 框架", author: "小鱼儿", date: "2014-04-23", imgUrl: "/images/dt.png", content: vueText},
        {id: 2, title: "一套框架多种平台 移动端&桌面端", author: "花无缺", date: "2015-05-24", imgUrl: "/images/dt.png", content: reactText}
    ]

    //插入数据
    data.forEach(item => {
        let sql = `insert into article  values 
        (${item.id}, '${item.title}', '${item.author}',
        '${item.date}', '${item.imgUrl}', '${item.content}')`

        db.query(sql, (err, result) => {
            if(err) throw err
            console.log(result)
        })
    })
}

fn()


/*
//插入咨询数据
const arr = [
    {id: 0, icon: '/images/angular.gif', subtitle: "学会用 Angular 构建应用，把这些代码和能力复用在多种不同平台的应用上", title: "一套框架多种平台 移动端&桌面端"},
    {id: 1, icon: '/images/vue.gif', subtitle: "不断繁荣的生态系统，可以在一个库和一套完整框架之间自如伸缩", title: "渐进式的JavaScript 框架"},
    {id: 2, icon: '/images/react.gif', subtitle: "组件逻辑使用 JavaScript 编写而非模版，你可以轻松地在应用中传递数据，并使得状态与 DOM 分离", title: "用于构建用户界面的 JavaScript 库"},
]

arr.forEach(item => {
    let sql = `insert into zixun values (${item.id}, '${item.icon}', '${item.subtitle}',
    '${item.title}')`

    query(sql, (err, result) => {
        if(err) throw err

        console.log(result)
    })
})

*/